规范化工具
EditorConfig
用来统一不同平台不同编辑器中的编辑内容规范的配置,被各种编辑器广泛的支持,一次配置处处统一的感觉
配置样例 .editorconfig
1 | # 项目根目录下的这个值要 为true, 配置这一个就可以用了 |
文件格式
- 空行 只包含空格
- 注释
;
或者#
开头的行 - 区块头
[
开头]
结尾,/
为路径分隔符,\\
不能为路径分隔符 - 键值对 A=B的形式,中间可以有空格
匹配规则
*
匹配不带/
的字符串**
匹配任意字符串?
匹配非/
的单字符[seq]
任意中括号内的单字符[!seq]
任意非中括号内的单字符{s1,s2,s3}
任意单个字符串{num1,num2}
任意一个范围内的整数
配置属性
所有属性值都是大小写不敏感的,都会被核心库转为小写
root
true
表示这个是顶层配置
indent_style
tab
space
indent_size
tab
用tab_width
或 tab的宽度- 列的数量
tab_width
- 列的数量,当
indent_size
是数字的时候,默认是indent_size
- 列的数量,当
end_of_line
不设置就可以不同平台用不同平台的,代码管理平台来控制lf
crlf
cr
charset
latin
utf-8
utf-16be
utf-16le
utf-8-bom
trim_trailing_whitespace
true
false
insert_final_newline
true
false
ESLint
冲破一切,最后胜出的一款ECMAScript及其周边的静态规范检查工具
安装配置
1 | npm i eslint -g # 全局安装 |
配置方式
注释方式,可以直接配置到源代码中
配置文件的方式,可以使用
JavaScript
,JSON
或者YAML
文件来进行配置- 可以使用
.eslintrc.*
文件来配置,也可以在package.json
中的eslintConfig
中配置 - 同一个目录下的 优先级顺序
.eslintrc.js
.eslintrc.yaml
.eslintrc.yml
.eslintrc.json
.eslintrc
package.json
- 可以使用
禁用规则
- 注释的方式
- 禁用一组文件的配置文件中的规则,使用
rules
,overrides
中再配置rules来做
忽略的文件
.eslintignore
#
开头的行被当做注释
路径是相对于本配置文件的路径
- 忽略模式的规范跟
.gitignore
一致 gitignore
- 忽略模式的规范跟
以
!
开头的为否定模式,重新包含前面被忽略的模式默认lint
.js
文件,其他的需要根据命令行参数指定1
eslint --fix-dry-run --no-ignore --ext .js,.jsx .
配置属性
parser
常用默认的和babel-eslint
parserOptions
解析器的信息-
ecmaVersion
这个字段不会自动启动比如ES6的全局变量 sourceType
默认script
可以设为module
ecmaFeatures
额外的语言特性globalReturn
impliedStrict
全局严格模式jsx
experimentalObjectRestSpread
实验的特性
-
plugins
插件可以提供处理器overrides
插件重写的配置processor
可以从另一种文件中提取代码,然后处理其中的JavaScriptenv
检查所支持的环境,browser
,node
,commonjs
,es6
等globals
全局变量- 每个变量的属性值
writable
,readonly
,off
- 每个变量的属性值
rules
具体的规则- 每一个规则的值
off
,warn
,error
或者0
,1
,2
- 配置在插件中的规则时,需要是
插件名/ 规则ID
的形式 - 规则配置文档
- 每一个规则的值
extends
扩展配置eslint:recommended
扩展推荐的配置
StyleLint
通用的样式规则检查工具
1 | npm install --save-dev stylelint stylelint-config-standard |
禁用规则
1 | /* stylelint-disable */ |
配置Prettier
非常受欢迎的代码美化工具
1 | npm i eslint-config-prettier -D |
- 配置改到
.eslintrc.js
中的extends
序列中
配置lint-staged
1 | npm i lint-staged -D |
在package.json
中添加如下配置
1 | "lint-staged": { |
- 匹配规则
- 使用micromatch 来匹配
*.js
所有的js文件!(*test).js
除了*test.js
的js文件- 如果不带
/
就不管路径,如果带/
就需要管路径 - 可以通过
*.{js,jsx}
来指定多个匹配后缀
配置 commitlint
安装
1
npm install --save-dev @commitlint/{cli,config-conventional}
配置文件
commitlint.config.js
1
2
3module.exports = {
extends: ['@commitlint/config-conventional']
}
配置husky
1 | npm i husky -D |
在package.json
中加入如下配置
1 | "husky": { |
编辑器配置
WebStorm 或 IDEA系列的
配置EditorConfig
【Preference】=>【Editor】=> 【Code Style】=> [Enable EditorConfig Support]
配置ESLint
【Preference】=> 【Language & Frameworks】=>【JavaScript】=> 【Code Quality Tools】=> 【ESLint】=> [Automatic ESLint Configuation]
VSCode 配置
配置EditorConfig
【Extention】=>搜索[EditorConfig for VSCode] 然后 【install】即可
配置ESLint
【Extention】=>搜索[ESLint] 然后 【install】即可
项目实践操作
EditorConfig
拷贝如下内容到项目.editorconfig
文件中,
1 | # 项目根目录下的这个值要 为true, 配置这一个就可以用了 |
配置ESLint
使用 eslint --init
并选择对应的选项 ,生成 基础的配置
- 增加基础的lint配置
npm i eslint-config-recommended -D
然后在.eslintrc.js
的extends
中把 eslint:recommended
放到第一个
增加对 babel的支持
npm i babel-eslint -D
然后在eslint
的配置文件里面增加"parser":"babel-eslint"
的属性配置
配置Stylelint
小程序配置
1 | npm install --save-dev stylelint stylelint-config-standard stylelint-config-standard-wxss |
配置文件
.stylelintrc.json
1
2
3{
"extends": "stylelint-config-standard-wxss"
}配置文件
.stylelintignore
1
2node_modules
miniprogram_npm
React工程配置
1 | npm install --save-dev stylelint stylelint-config-standard stylelint-scss |
配置文件
.stylelintrc.json
1
2
3
4{
"extends": "stylelint-config-standard",
"plugins": ["stylelint-scss"]
}配置文件
.stylelintignore
1
2node_modules
dist
配置lint-staged
npm i lint-staged -D
然后就可以在package.json
中添加对应的配置了
1 | "lint-staged": { |
配置 commitlint
安装
1
npm install --save-dev @commitlint/{cli,config-conventional}
配置文件
commitlint.config.js
1
2
3module.exports = {
extends: ['@commitlint/config-conventional']
}
配置husky
npm i husky -D
然后就可以在package.json
中添加hooks了
1 | "husky": { |
参考文档
最后更新: 2022年03月02日 03:32
原始链接: http://rawbin-.github.io/dev-env/2018-10-01-project-normalize/